package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Unit;
import com.atguigu.jxc.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @description 商品信息Controller
 */
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    // 首页
    // 查询当前库存（可根据商品类别、商品编码或名称搜索）
    // http://localhost:8080/goods/listInventory
    @PostMapping("/goods/listInventory")
    public Map<String, Object> getIndexGoodsByPageAndCondition(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        // 开启分页功能
        // 注意参数传递：将数据通过ThreadLocal绑定到当前线程上，传递给后续流程（dao）使用
        PageHelper.startPage(page, rows);
        PageInfo<Goods> pageInfo = goodsService.getIndexGoodsByPageAndCondition(codeOrName, goodsTypeId);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("rows", pageInfo.getList());
        return resultMap;
    }

    // 查询所有商品单位
    // http://localhost:8080/unit/list
    @PostMapping("/unit/list")
    public Map<String, Object> getAllUnits() {
        Map<String, Object> resultMap = new HashMap<>();
        List<Unit> unitList = goodsService.getAllUnits();
        resultMap.put("rows", unitList);
        return resultMap;
    }

    // 查询所有商品信息
    // http://localhost:8080/goods/list
    @PostMapping("/goods/list")
    public Map<String, Object> getGoodsByPageAndCondition(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        // 开启分页功能
        // 注意参数传递：将数据通过ThreadLocal绑定到当前线程上，传递给后续流程（dao）使用
        PageHelper.startPage(page, rows);
        PageInfo<Goods> pageInfo = goodsService.getGoodsByPageAndCondition(goodsName, goodsTypeId);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("rows", pageInfo.getList());
        return resultMap;
    }

    // 商品添加或修改
    // http://localhost:8080/goods/save?goodsId=37
    @PostMapping("/goods/save")
    @SuppressWarnings("rawtypes")
    public ServiceVO addOrUpdateGoods(Integer goodsId, Goods goods) {
        if (Objects.isNull(goodsId)) {
            goodsService.saveGoods(goods);
        } else {
            goods.setGoodsId(goodsId);
            goodsService.updateGoodsById(goods);
        }
        return new ServiceVO(100, "请求成功");
    }

    // todo 做条件判断
    // 商品删除（要判断商品状态,入库、有进货和销售单据的不能删除）
    // http://localhost:8080/goods/delete
    @PostMapping("/goods/delete")
    @SuppressWarnings("rawtypes")
    public ServiceVO deleteGoods(Integer goodsId) {
        boolean isSuccess = goodsService.deleteGoodsById(goodsId);
        if (!isSuccess) {
            return new ServiceVO(404, "删除失败，存在关联信息");
        }
        return new ServiceVO(100, "请求成功");
    }

    // 无库存商品列表展示（可以根据商品名称或编码查询）
    // http://localhost:8080/goods/getNoInventoryQuantity
    @PostMapping("/goods/getNoInventoryQuantity")
    public Map<String, Object> getNoStockGoodsByPageAndCondition(Integer page, Integer rows, String nameOrCode) {
        // 开启分页功能
        // 注意参数传递：将数据通过ThreadLocal绑定到当前线程上，传递给后续流程（dao）使用
        PageHelper.startPage(page, rows);
        PageInfo<Goods> pageInfo = goodsService.getNoStockGoodsByPageAndCondition(nameOrCode);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("rows", pageInfo.getList());
        return resultMap;
    }

    // 有库存商品列表展示（可以根据商品名称或编码查询）
    // http://localhost:8080/goods/getHasInventoryQuantity
    @PostMapping("/goods/getHasInventoryQuantity")
    public Map<String, Object> getHasStockGoodsByPageAndCondition(Integer page, Integer rows, String nameOrCode) {
        // 开启分页功能
        // 注意参数传递：将数据通过ThreadLocal绑定到当前线程上，传递给后续流程（dao）使用
        PageHelper.startPage(page, rows);
        PageInfo<Goods> pageInfo = goodsService.getHasStockGoodsByPageAndCondition(nameOrCode);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("rows", pageInfo.getList());
        return resultMap;
    }

    // 添加库存、修改数量或成本价
    // http://localhost:8080/goods/saveStock?goodsId=25
    @PostMapping("/goods/saveStock")
    @SuppressWarnings("rawtypes")
    public ServiceVO setInventoryQuantityAndPurchasingPrice(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        goodsService.setInventoryQuantityAndPurchasingPrice(goodsId, inventoryQuantity, purchasingPrice);
        return new ServiceVO(100, "请求成功");
    }

    // todo
    // 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
    // http://localhost:8080/goods/deleteStock
    @PostMapping("/goods/deleteStock")
    @SuppressWarnings("rawtypes")
    public ServiceVO deleteStockById(Integer goodsId) {
        boolean isSuccess = goodsService.deleteStockById(goodsId);
        if (!isSuccess) {
            return new ServiceVO(404, "删除失败，存在关联信息");
        }
        return new ServiceVO(100, "请求成功");
    }

    /**
     * 生成商品编码
     *
     * @return
     */
    @RequestMapping("/goods/getCode")
    @RequiresPermissions(value = "商品管理")
    public ServiceVO getCode() {
        return goodsService.getCode();
    }
}
